package servlet.classes;

import com.alibaba.fastjson.JSONObject;
import po.Class_table;
import utils.JdbcUtil;
import utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *     类别信息列表
 */
@WebServlet(urlPatterns = "/classList")
public class ClassList extends HttpServlet {
    JdbcUtil jdbc =new JdbcUtil();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String c_name =req.getParameter("classesname");

        Connection connection = jdbc.getConnection();
        String sql = "select * from classes_table where 1=1";
        PreparedStatement preparedStatement = null;

        //获取当前页和条数
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        //计算分页条数
        Integer curent = (page-1)*limit;


        try {
            // 记录当前占位符顺序
            int i =0;
            //动态SQL拼接
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(sql);

            if (c_name!=null && c_name.trim().length()!=0){
                stringBuffer.append(" and classesname like ?");
            }
            preparedStatement = connection.prepareStatement(stringBuffer.toString());

            if (c_name!=null && c_name.trim().length()!=0){
                ++i;
                preparedStatement.setString(i,"%"+c_name+"%");
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            //光标最后一位
            resultSet.last();

            Integer count = resultSet.getRow();
            //分页sql
            stringBuffer.append(" limit ?,?");
            preparedStatement  = connection.prepareStatement(stringBuffer.toString());
            //设置分页参数占位符
            i =0;
            if (c_name!=null && c_name.trim().length()!=0){
                ++i;
                preparedStatement.setString(i,"%"+c_name+"%");
            }
            ++i;
            preparedStatement.setInt(i,curent);
            ++i;
            preparedStatement.setInt(i,limit);

             resultSet = preparedStatement.executeQuery();
            List<Class_table> classLists = new ArrayList<>();

            while (resultSet.next()){
                Class_table table = new Class_table();
                table.setClassesname(resultSet.getString("classesname"));
                table.setRemark(resultSet.getString("remark"));
                table.setId(resultSet.getInt("id"));
                classLists.add(table);

            }

            ResultData page1 = ResultData.page(classLists,count.longValue());
            Object o = JSONObject.toJSON(page1);
            resp.getWriter().println(o.toString());




        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }




    }
}
